﻿using E_GovernmentSystem.Models;
using Microsoft.AspNetCore.Mvc;
using NPOI.XWPF.UserModel;

namespace E_GovernmentSystem.Controllers
{
    public class UserInfoController : Controller
    {
        //上下文类
        private readonly SqlSugar.ISqlSugarClient db;

        public UserInfoController(SqlSugar.ISqlSugarClient _db)
        {
            this.db = _db;
        }

        public IActionResult Index()
        {
            //获取部门信息
            List<department> departmentList = db.Queryable<department>().ToList();
            ViewData["departmentList"] = departmentList;

            //获取角色信息
            List<role> roleList = db.Queryable<role>().ToList();
            ViewData["roleList"] = roleList;

            return View();
        }

        //获取员工信息
        public IActionResult getUserInfoList(int page, int limit, string strName)
        {
            int total = 0;

            //联表查询
            var query5 = db.Queryable<usertable>()
                     .LeftJoin<department>((a, b) => a.DepartmentId == b.DepartmentId)
                     .Where(a => (string.IsNullOrEmpty(strName) || a.UserName.Contains(strName)))
                     .OrderByDescending(a => a.CreationTime)
                     .Select((a, b) => new
                     {
                         a.UrseId,
                         a.UserCode,
                         a.DepartmentId,
                         a.UserName,
                         a.CreationTime,
                         a.UserSatus,
                         a.UserPassword,
                         a.UserLevel,
                         a.UserPosition,
                         a.AgentNo,
                         a.AgentPasswore,
                         a.AgentTelephone,
                         a.Contact,
                         b.DepartmentName
                     }).ToPageList(page, limit, ref total);

            var json = new
            {
                code = 0,
                data = query5,
                count = total,
                msg = ""
            };
            return Json(json);
        }



        //保存
        public IActionResult Save(ChildUserInfoModel info)
        {
            usertable infoss = new usertable
            {
                DepartmentId = info.DepartmentId,
                UserName = info.UserName,
                AgentNo = info.AgentNo,
                AgentPasswore = info.AgentPasswore,
                AgentTelephone = info.AgentTelephone,
                Contact = info.Contact,
                CreationTime = DateTime.Now.ToString("yyyy-MM-dd"),
                UserCode = info.UserCode,
                UrseId = info.UrseId,
                UserLevel = info.UserLevel,
                UserPassword = "123456",
                UserPosition = info.UserPosition,
                UserSatus = info.UserSatus
            };

            bool res = (db.Storageable(infoss).DefaultAddElseUpdate().ExecuteCommand()) > 0 ? true : false;

            if (res)
            {
                int? userid = db.Queryable<usertable>().OrderByDescending(a => a.UrseId).First().UrseId;
                userrole infos = new userrole()
                {
                    UserId = userid,
                    RoledId = info.RoleId
                };
                db.Insertable(infos).ExecuteCommand();
            }
            return Json(res);
        }

        //删除
        [HttpPost]
        public IActionResult Delete(string ids)
        {
            bool res = false;
            if (!string.IsNullOrEmpty(ids))
            {
                if (ids.Contains(','))
                {
                    string[] idArr = ids.Split(',');
                    int[] intArr = Array.ConvertAll(idArr, int.Parse);
                    res = db.Deleteable<usertable>().In(intArr).ExecuteCommand() > 0;
                }
                else
                {
                    int id = int.Parse(ids);
                    res = db.Deleteable<usertable>().In(id).ExecuteCommand() > 0;
                }
            }
            return Json(res);
        }
    }
}
